package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwadT;
import cn.e4j.modular.crm.entity.GwadlT;
import cn.e4j.modular.crm.mapper.GwadTMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 区域别维护 服务实现类
 *
 * @author yuan_rz
 * @since 2019-01-11
 */
@Service
public class Gwai220ServiceImpl extends ServiceImpl<GwadTMapper, GwadT>{

    @Transactional(readOnly = true)
    public List<Dto> getListData(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getListData(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getAreaList(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getAreaList(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Boolean existedTheParams(GwadT gwadT) {
        QueryWrapper<GwadT> gwadTQueryWrapper = new QueryWrapper<>();
        gwadTQueryWrapper.eq("GWADENT",gwadT.getGWADENT());
        gwadTQueryWrapper.eq("GWAD001",gwadT.getGWAD001());
        gwadTQueryWrapper.eq("GWAD002",gwadT.getGWAD002());
        return this.baseMapper.selectList(gwadTQueryWrapper).size() > 0;
    }

    @Transactional
    public void addData(GwadT gwadT, GwadlT gwadlT){
        if("".equals(gwadT.getGWAD003()) || gwadT.getGWAD003()==null){
            gwadT.setGWAD003(" ");
        }
        gwadT.insert();
        gwadlT.insert();
    }

    @Transactional
    public void editData(GwadT gwadT, GwadlT gwadlT,GwadT oldGwadT){
        if("".equals(gwadT.getGWAD003()) || gwadT.getGWAD003()==null){
            gwadT.setGWAD003(" ");
        }
        //修改1
        UpdateWrapper<GwadT> gwadTUpdateWrapper = new UpdateWrapper<>();
        gwadTUpdateWrapper.eq("GWADENT",oldGwadT.getGWADENT());
        gwadTUpdateWrapper.eq("GWAD001",oldGwadT.getGWAD001());
        gwadTUpdateWrapper.eq("GWAD002",oldGwadT.getGWAD002());
        //修改2
        UpdateWrapper<GwadlT> gwadlTUpdateWrapper = new UpdateWrapper<>();
        gwadlTUpdateWrapper.eq("GWADLENT",oldGwadT.getGWADENT());
        gwadlTUpdateWrapper.eq("GWADL001",oldGwadT.getGWAD001());
        gwadlTUpdateWrapper.eq("GWADL002",gwadlT.getGWADL002());
        gwadlTUpdateWrapper.eq("GWADL003",gwadlT.getGWADL003());

        gwadT.update(gwadTUpdateWrapper);
        gwadlT.update(gwadlTUpdateWrapper);
    }

    @Transactional
    public void deleteData(GwadT gwadT, GwadlT gwadlT){
        delete(gwadT,gwadlT);
    }

    @Transactional
    public void deleteDataBatch(List<GwadT> gwadTList,List<GwadlT> gwadlTList){
        for(int i=0; i < gwadTList.size(); i++){
            delete(gwadTList.get(i),gwadlTList.get(i));
        }
    }

    private void delete(GwadT gwadT, GwadlT gwadlT){
        //修改1
        UpdateWrapper<GwadT> gwadTUpdateWrapper = new UpdateWrapper<>();
        gwadTUpdateWrapper.eq("GWADENT",gwadT.getGWADENT());
        gwadTUpdateWrapper.eq("GWAD001",gwadT.getGWAD001());
        gwadTUpdateWrapper.eq("GWAD002",gwadT.getGWAD002());
        //修改2
        UpdateWrapper<GwadlT> gwadlTUpdateWrapper = new UpdateWrapper<>();
        gwadlTUpdateWrapper.eq("GWADLENT",gwadlT.getGWADLENT());
        gwadlTUpdateWrapper.eq("GWADL001",gwadlT.getGWADL001());
        gwadlTUpdateWrapper.eq("GWADL002",gwadlT.getGWADL002());
        gwadlTUpdateWrapper.eq("GWADL003",gwadlT.getGWADL003());

        gwadT.delete(gwadTUpdateWrapper);
        gwadlT.delete(gwadlTUpdateWrapper);
    }


}
